package com.example.androidclass;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class Timeline extends AppCompatActivity {

    private ArrayList<OneDay> day_list;
    private ArrayList<ArrayList<OneActivity>> all_activity_list;
    private ArrayList<OneActivity> one_activity_list;
    private Context context;
    private ExpandableListView expandList_timeline;
    private MyBaseExpandableListAdapter myAdapter;
    private ImageView add_activity_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);
        context = Timeline.this;
        expandList_timeline = (ExpandableListView) findViewById(R.id.timeline);
        add_activity_button = findViewById(R.id.add_activity);
        add_activity_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Timeline.this, AddActivity.class);
                startActivity(intent);
            }
        });



//        隐藏默认标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        List<Schedule.oneClass> class_list = Schedule.class_list;
        day_list = new ArrayList<OneDay>();
        all_activity_list = new ArrayList<ArrayList<OneActivity>>();

        String temp = null;
        for (Schedule.oneClass one_class : class_list) {
//            判断是否为新的一天
            if (day_list.isEmpty() || !day_list.get(day_list.size() - 1).getDate().equals(one_class.getDate())) {
                if (!day_list.isEmpty()) {
                    temp = day_list.get(day_list.size() - 1).getDate();
                }
//                创建新的表头
                day_list.add(new OneDay(one_class.getDay(), one_class.getDate()));
//                创建新的组
                if (!one_class.getDate().equals(temp)) {
                    one_activity_list = new ArrayList<OneActivity>();
                    all_activity_list.add(one_activity_list);
                }
            }
            one_activity_list.add(new OneActivity(one_class.getName(), one_class.getClassroom(), one_class.getReal_time()));
        }

        myAdapter = new MyBaseExpandableListAdapter(day_list, all_activity_list, context);
        expandList_timeline.setAdapter(myAdapter);
//        取消group的点击隐藏事件
        expandList_timeline.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
//        启动时默认展开group
        int count = expandList_timeline.getCount();
        for (int i = 0; i < count; i++) {
            expandList_timeline.expandGroup(i);
        }
    }

}
